<?php

namespace App\Controller\App;

use App\Base\BaseCache;
use App\Base\BaseController4App;
use App\Base\Format;
use App\Entity\PayLog;
use App\Entity\User;
use App\Model\LazyCommon\MiniAppModel;
use App\Model\LazyCommon\RulesModel;
use App\Model\LazyCommon\UserModel;
use App\Model\LazyCommon\WechatPayModel;
use App\Model\LiveModel;
use App\Model\PayLogModel;
use App\Model\TopicModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

/**
 * @route("/pay/cb")
 */
class PayCallBackController extends BaseController4App
{
    /**
     * 接口地址是md5(congmingwechat) WechatPayModel 46行的配置需要一致
     * /pay/cb/28bc121ddd6a5e727337c5f4a7510192
     * @route("/28bc121ddd6a5e727337c5f4a7510192",name="WeChatPayCallBack")
     */
    public function WeChatPayCallBack()
    {
        $postStr  = file_get_contents('php://input');
        //禁止引用外部xml实体
        $bPreviousValue = libxml_disable_entity_loader(true);
        $array_data = json_decode(json_encode(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        libxml_disable_entity_loader($bPreviousValue);
        if(isset($array_data['return_code']) and  $array_data['return_code'] == 'SUCCESS' ){
            file_put_contents("./../FileData/log_pay_cb.txt","array_data:".json_encode($array_data)."\n\n",FILE_APPEND);
            $cbCode = $array_data["out_trade_no"];//取出回调码
            $PayLogModel = new PayLogModel($this->getDoctrine());
            $PayLogModel->dealWithCallBack($cbCode);
        }
        echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';die;
    }

    /**
     * 测试地址
     *
     * @route("/test",name="TestAutoCb")
     */
    public function TestAutoCb()
    {
        //todo 非常危险，线上删除
        if ($sn = $this->getParamRequired("sn")) {
            $PayLogModel = new PayLogModel($this->getDoctrine());
            $PayLogModel->dealWithCallBack($sn);
            return $this->json_out(1,"已回调");
        }else{
            return $this->json_out(0,"sn错误");
        }
    }


}
